import { queryRule } from '@/services/list'; 
import {ListApi} from '@/interface/index'
 

 
const ListModel:ListApi  = {
  namespace: 'List', 
  state: {
    data:[]
  },

  effects: {
    *fetch(payload, { call, put }) {
      const response = yield call(queryRule); 
      
      if(response.code === 0) {
        yield put({   
          type: 'SAVE',
          payload:response.rows,
        });
      }
    }, 
  },

  reducers: {
    SAVE(state, action) {
      return {
        ...state,
        data: action.payload
      };
    }, 
  },

  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/list') {
          dispatch({
            type: 'fetch',
          })
        }
      });
    }
  }
};

export default ListModel;
